import user from '../../utils/user'
import {
    dateFormat
} from '../../utils/util'
import Socket from '../../api/socket'
// import {
//     getInvitePeople
// } from '../../api/app.js'
Component({
    properties: {
        projectId: String,
        joinType: String
    },
    data: {
        count:0,
        workers:[],
        timer: ''
    },
    
    ready() {
        console.log(this.data.projectId,'this.projectId')
        // 获取通过邀请码进入的列表
        // const time = dateFormat('YYYY-mm-dd HH:MM:SS', new Date())
        this.setData({
            socket: new Socket('/api/v2/webSocket/'+this.data.projectId+'/'+user.userId ,(message)=>{
                try{
                    const data = JSON.parse(message.data)
                    console.log('data',data)
                    if(data.type === 'add'){
                        this.data.workers.push({
                            userHeadUrl: data.data.userHeadUrl,
                            userName: data.data.workerName,
                            createTime: dateFormat('YYYY-mm-dd HH:MM:SS',new Date())
                        })
                        this.setData({
                            count: this.data.count + 1,
                            workers: this.data.workers
                        })
                    }
                }catch(e){
                    console.log(message,'message')
                }
            })
            // // 获取通过邀请码进入的列表
            // timer:setInterval(() => {
            //     getInvitePeople({
            //         projectId: this.data.projectId,
            //         createTime: time,
            //         joinType: this.data.joinType
            //     }).then(res => {
            //         console.log(res)
            //         this.setData({
            //             count: res.count,
            //             workers: res.info
            //         })
            //     })
            // }, 1000)
        })

    },
    // reCode() {
    //     this.createInviteCode(true)
    // },
    detached:function(){ 
        console.log(this.data.socket,'this.data.socket')
        this.data.socket.close()
        // clearInterval(this.data.timer)
    }
})